/**
 *
 * @description: 调用wechat-lib/index.js文件的access_token
 * @author: junyong.hong
 * @createTime: 2018/10/11
 * @version: 1.0.0.0
 * @history:
 *    1、
 *    2、
 *
 */
const Wechat = require('../wechat-lib/index')
// 网页授权
const WechatOAuth = require('../wechat-lib/oauth')
const config = require('../config/config')
const mongoose = require('mongoose')

// 通过mongoose获取到token的数据模型
const Token = mongoose.model('Token')
const Ticket = mongoose.model('Ticket')

const wechatCfg = {
    wechat: {
        appId: config.wechat.appID,
        appSecret: config.wechat.appSecret,
        token: config.wechat.token,
        getAccessToken: async () => {
            // 调用实体里的静态方法
            const res = await Token.getAccessToken()

            return res
        },
        saveAccessToken: async (data) => {
            // 调用实体里的静态方法
            const res = await Token.saveAccessToken(data)

            return res
        },
        getTicket: async () => {
            const res = await Ticket.getTicket()

            return res
        },
        saveTicket: async (data) => {
            const res = await Ticket.saveTicket(data)

            return res
        }
    }
}

// 请勿删除
// exports.getWechat = async () => {
//     const client = new Wechat(wechatCfg.wechat)
//
//     return client
// }

// 微信获取token(等同上面写法)  健壮函数写法
// 调用wechat-lib/index
exports.getWechat = () => new Wechat(wechatCfg.wechat)

// 网页授权
// 调用wechat-lib/oauth
exports.getOAuth = () => new WechatOAuth(wechatCfg.wechat)